/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;



import Entidad.Compra;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
/**
 *
 * @author Stiv
 */
public class CompraDAO {
    
    private static EntityManagerFactory emf=Persistence.createEntityManagerFactory("CRMPU");



    public boolean crear(Compra object) {

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        boolean salida=false;
        try {
            em.persist(object);
            em.getTransaction().commit();
            salida = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
            return salida;
        }
    }

    
    public boolean eliminar(Compra object) {

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Compra com = em.find(Compra.class, object.getID());
        boolean ret = false;
        try {
            em.remove(com);
            em.getTransaction().commit();
            ret = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
            return ret;
        }
    }


    public Compra leer(Compra par) {

        EntityManager em = emf.createEntityManager();
        Compra compra = em.find(Compra.class, par.getID());
        return compra;
    }

        
    public Compra leer(int par) {

        EntityManager em = emf.createEntityManager();
        Compra compra = em.find(Compra.class, par);
        return compra;
    }
  
    public boolean actualizar(Compra object, Compra nuevoObjeto) {

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        boolean ret = false;
        try {
            object.setID(nuevoObjeto.getID());
            object.setFecha(nuevoObjeto.getFecha());
            object.setValor(nuevoObjeto.getValor());
            object.setProducto(nuevoObjeto.getProducto());
            em.merge(object);
            em.getTransaction().commit();
            ret = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
            return ret;
        }
    }

    public ArrayList<Integer> listar (){
        ArrayList<Integer> ids = new ArrayList<Integer>();

        EntityManager em = emf.createEntityManager();
        List lista = null;
        Query q = em.createQuery("SELECT c from Compra c ");
        try {
            lista = q.getResultList();
            for (int i=0; i<lista.size(); i++){
                Compra p = (Compra)lista.get(i);
                ids.add(p.getID());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
            return ids;
        }
    }
    
    
    //Metodo para traer todas las compras
    public List<Compra> bringAll(){
        EntityManager em = emf.createEntityManager();
        List<Compra> cat = new ArrayList<Compra>();
        
        Query q= em.createQuery("SELECT u FROM Compra u");
        List<Compra> tempCat = q.getResultList();
        try {
        for(Compra c: tempCat){
            Compra cate = new Compra();
            cate.setID(c.getID());
            cate.setFecha(c.getFecha());
            cate.setValor(c.getValor());
            cate.setProducto(c.getProducto());
            cat.add(cate);
        }
        
        }
        catch(Exception e){
            e.printStackTrace();;            
        }
        finally {
            
            em.close();
            return cat;
        }
    }
    
}
